﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ArtClub.Model;
using ArtClub.Controller;

using MAuction = ArtClub.Model.Auction;

namespace UI
{
	/// <summary>
	/// Interaction logic for Auction.xaml
	/// </summary>
	public partial class Auction : UserControl
	{
		#region Fields

		private MemberCtr memberCtr = new MemberCtr();
		private IAuctionCtr auctionCtr = new AuctionCtr();

		#endregion

		public Auction()
		{
			InitializeComponent();
			UpdateAuctionList();
		}

		private MAuction SelectedAuction { get; set; }
		private Member SelectedMember { get; set; }

		public void UpdateAuctionList()
		{
			AuctionsListView.ItemsSource = auctionCtr.ListAuctions();
		}

        private void AttachPicture()
        {
            try
			{
                ImageName.Source = new BitmapImage(new Uri(textboxArtDirection.Text));
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
        }

		private void SubmitBid_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				if (SelectedAuction == null)
					throw new Exception("No auction selected!");
				if (SelectedMember == null)
					throw new Exception("No member selected!");

				// Create a new bid.
				Bid b = new Bid()
				{
					Bidder = (Member)MemberList.SelectedItem,
					BidPrice = double.Parse(textboxBid.Text)
				};
				// Commit the bid.
				auctionCtr.Update(SelectedAuction, b);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				return;
			}

			UpdateAuctionList();

			// Refresh highest bidder.
			UpdateHighestBidder();

			UpdateMemberList();
		}

		private void UpdateHighestBidder()
		{
			// Get the highest bid.
			Bid b = (from n in SelectedAuction.Bids
					 orderby n.BidPrice descending
					 select n).FirstOrDefault();

			if (b != null)
			{
				// Update textfields.
				textboxBiddersName.Text = b.Bidder.Name;
				textboxCurrentBid.Text = b.BidPrice.ToString();
			}
		}

		private void UpdateSelectedMember()
		{
			if (SelectedMember != null)
			textboxMemberNumber.Text = SelectedMember.Id.ToString();
			else
				textboxMemberNumber.Text = "";
		}

		public void UpdateMemberList()
		{
			//MemberList.ItemsSource = auctionCtr.ListMembersByPoint(SelectedAuction.Id);
			MemberList.ItemsSource = memberCtr.ListMembers();
		}

		private void AuctionsListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			SelectedAuction = (MAuction)AuctionsListView.SelectedItem;

			MAuction a = (MAuction)AuctionsListView.SelectedItem;

			textboxArtName.Text = a.AuctionItem.Name;
			textboxDescription.Text = a.AuctionItem.Description;
			textboxMinPrice.Text = a.AuctionItem.Price.ToString();
            textboxArtDirection.Text = a.AuctionItem.Direction;
            AttachPicture();

			UpdateMemberList();
			UpdateHighestBidder();
		}

		public void MemberList_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			SelectedMember = (Member) MemberList.SelectedItem;
			UpdateSelectedMember();
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			auctionCtr.Close(SelectedAuction);

			UpdateAuctionList();
		}
	}
}
